<?php

class home extends CI_Controller{
	
	private  $_date_menu;
	private  $_category;
	private  $_comments;
	
	function __construct()
	 {
	    parent::__construct();	
	    $this->load->database();
	    $this->load->helper(array('url','form'));
	    $this->myInit();
	 }
	 
	/*     
	 * 生成右则栏的日期菜单
	*/
	function myInit()
	{
	    $date = $this->db->select('created')->get('articles')->result_array();
		foreach($date as $d){
			$date_menu[] = date("Y-m",$d['created']);  //只生成年和月
		}
	   	$this->_date_menu = array_unique($date_menu); //去除重复的菜单，只保留一个。
	   	$this->_category = $this->db->select('*')->get('category')->result_array();
	   	$this->_comments = $this->db->select('*')->limit(12)->order_by('cid','desc')->get("comment")->result_array();
	}
	 
	public function index()
	{
		/*     分页代码          */
	    $num = 4;    //每页显示数量
		if($this->uri->segment(3) == FALSE)
		{
			$offset = 0;
		}else{
			if($this->uri->segment(3) ==1)
			{
				$offset=0;
			}else{
			$offset = $this->uri->segment(3)*$num-$num;   //计算篇移量
			} 
		}
	   
	   $counts = $this->db->count_all('articles');
	   $count_page = ceil($counts/$num);
       $data['count_page'] = $count_page;  //分页的总页数
	   $data['url'] = base_url('home/index');
        
	   $this->db->select('id,category,title,body,tags,own,visits,created');
	   $this->db->limit($num, $offset);   
	   $this->db->order_by('id','desc');

	   $articles = $this->db->get('articles')->result_array();
	   $new_articles = array();
	   if(!empty($articles))
	   {
	   	   foreach($articles as $row)
	   	   {
	   	   	   if(!empty($row['tags']))
	   	   	   {
	   	   	   	  $row['tags'] = explode(',', $row['tags']);
	   	   	   }
	   	   	   $new_articles[]=$row;
	   	   }
	   }
	   $data['articles'] = $new_articles;
	   $data['count_page'] = $count_page;

	   $data['category'] = $this->_category;  //获取所有文章分类
	   $data["comments"]  = $this->_comments;   //获取最新评论
	   $data['date_menu'] = $this->_date_menu;   //文章日期归档
        
	   $this->load->view('home/header');
	   $this->load->view('home/index',$data);
	   $this->load->view('home/right',$data);
	   $this->load->view('home/footer');
		
		
	}
	
   public function date()
	{
		//echo "hello";
		$getdate = urldecode($this->uri->segment(3));   //这真是个让人蛋痛的问题  URL中有中文的时候在IIS上出现了问题
		echo $getdate;
		//$getdate = iconv("gb2312","UTF-8",$getdate);    //   很明显在apache上这个是有问题的
		if($this->uri->segment(3) != FALSE){
			
            $date = str_replace(array('%2F'),array('-'),$getdate);
			$time = strtotime($date);
			
			$n = date('n', $time);
			
			//     分页代码         
		    $num = 4;    //每页显示数量
			if($this->uri->segment(4) == FALSE)
			{
				$offset = 0;
			}else{
				if($this->uri->segment(4) ==1)
				{
					$offset=0;
				}else{
				$offset = $this->uri->segment(4)*$num-$num;   //计算篇移量
				} 
			}
		   
		   $counts = $this->db->select('id')->where('MONTH(FROM_UNIXTIME(created))',$n)->get('articles')->result_array();  // 这个获取总记录数
		   $count_page = ceil(count($counts)/$num);
	       $data['count_page'] = $count_page;  //分页的总页数
		   $data['url'] = base_url('home/date').'/'.urlencode(iconv('UTF-8','gb2312',$getdate));   // 还有这里，也是关于URL中存在中文件
		    
			$data['articles'] = $this->db->select('*')->where('MONTH(FROM_UNIXTIME(created))',$n)->limit($num,$offset)->get('articles')->result_array();
		    
			$data['category'] = $this->_category;  //获取所有文章分类
	        $data["comments"]  = $this->_comments;   //获取最新评论
		    $data['date_menu'] = $this->_date_menu;   //文章日期归档
	        
		    $this->load->view('home/header');
		    $this->load->view('home/index',$data);
		    $this->load->view('home/right',$data);
		    $this->load->view('home/footer');
		}else{
			echo 'fial';
		} 
	}
	
	public function category()
	{
		
        if($this->uri->segment(3) == FALSE){
        	die("error");
        }
		/*     分页代码          */
	    $num = 4;    //每页显示数量
		if($this->uri->segment(4) == FALSE)
		{
			$offset = 0;
		}else{
			if($this->uri->segment(4) ==1)
			{
				$offset=0;
			}else{
			$offset = $this->uri->segment(4)*$num-$num;   //计算篇移量
			} 
		}
	   
	   $counts = $this->db->select('id')->where('category', $this->uri->segment(3))->get('articles')->result_array();
	   $counts = count($counts);
	   
	   $count_page = ceil($counts/$num);
       $data['count_page'] = $count_page;  //分页的总页数
       $data['url'] = base_url('home/category').'/'.$this->uri->segment(3);
       
		$this->db->select('*')->order_by('id','desc')->where('category',$this->uri->segment(3));
		$this->db->limit($num, $offset); 
		$data['articles'] = $this->db->get('articles')->result_array();	   
	
		$data['category'] = $this->_category;  //获取所有文章分类
	    $data["comments"]  = $this->_comments;   //获取最新评论
	    $data['date_menu'] = $this->_date_menu;   //文章日期归档
	   
	    $this->load->view('home/header');
	    $this->load->view('home/index',$data);
	    $this->load->view('home/right', $data);
	    $this->load->view('home/footer');	
	}
	
    public function show()
	{
		
		
		if($this->uri->segment(3) != 0)
		{
		  $this->db->select('*');
		  $article = $this->db->where('id',$this->uri->segment(3))->get('articles')->result_array();
		  if(count($article) == 0){
		       redirect('home/error');
		  }
		  /* 统计文章访问记录*/
		  $visits = $article[0]['visits'];
		  $data = array('visits'=>$visits+1);
		  $this->db->where('id',$this->uri->segment(3))->update('articles',$data);
		  
		  //获取该文章评论
		  $is_comment= $this->db->select('*')->where(array('id'=>$article[0]['id'],'is_show'=>1))->get('comment')->result_array();
          
		  $comment = array();
		  $sub_arr = array();
          
		  foreach($is_comment as $c)
		  {
		  	  if($c['pid'] == 0){
		  	  	 $comment[] = $c;
		  	  }else{
		  	  	$sub_arr[$c['pid']][] = $c;
		  	  }
		  }
		$result_array = array();
		foreach($sub_arr as $key=>$sub)
		{
		    foreach($comment as $k=>$v)
		    {
		    	if($v['cid'] == $key){
		    		$v['child'] = $sub;
		    		$result_array[] = $v;
		    	}
		    }	
		}
		
		  
		  /*   免验证码发表评论，擦啊，垃圾信息太多了。              */
		  $valicode = rand(10000,99999);
          $this->session->set_userdata('valicode', $valicode);
		  $data['valicode'] = $valicode;   
          $data['is_comment'] = $result_array;
          
		  $data['article'] = $article[0];
		  
		  $data['category'] = $this->_category;  //获取所有文章分类
	      $data["comments"]  = $this->_comments;   //获取最新评论
	      $data['date_menu'] = $this->_date_menu;   //文章日期归档
		 
		  $this->load->view('home/header',$data);
		  $this->load->view('home/show',$data);
		  $this->load->view('home/comment',$data);
		  $this->load->view('home/right',$data);
		  $this->load->view('home/footer');
		}
	}
	
	public function tag()
	{
	  if($this->uri->segment(3) != FALSE){
	  	/*     分页代码          */
	    $num = 4;    //每页显示数量
		if($this->uri->segment(4) == FALSE)
		{
			$offset = 0;
		}else{
			if($this->uri->segment(4) ==1)
			{
				$offset=0;
			}else{
			$offset = $this->uri->segment(4)*$num-$num;   //计算篇移量
			} 
		}
	   
	   $counts = $this->db->select('id')->like('tags', $this->uri->segment(3))->get('articles')->result_array();
	   $counts = count($counts);
	   
	   $count_page = ceil($counts/$num);
       $data['count_page'] = $count_page;  //分页的总页数
       $data['url'] = base_url('home/tag').'/'.$this->uri->segment(3);
       
	   $data['articles'] = $this->db->select('*')->order_by('id','desc')->like('tags',$this->uri->segment(3))->get('articles')->result_array();
	    
	   $data['category'] = $this->_category;  //获取所有文章分类
	   $data["comments"]  = $this->_comments;   //获取最新评论
	   $data['date_menu'] = $this->_date_menu;   //文章日期归档
	    
		$this->load->view('home/header');
	    $this->load->view('home/index',$data);
	    $this->load->view('home/right', $data);
	    $this->load->view('home/footer');
	  }
	}
	
	public function comment_post()
	{
		if($this->input->post())
		{
			$valicode = $this->input->post('valicode');
			$session_valicode = $this->session->userdata('valicode');
			if((!empty($valicode)) && $session_valicode){
				
				if($valicode == $session_valicode){
					$id = $this->input->post("id");
					$comment = array();
					$comment = $this->input->post();
					$comment['body'] = htmlspecialchars($comment['body']);
		 			$comment['created'] = time();
		 			
		 			unset($comment['valicode']);
					$this->db->insert('comment',$comment);
					redirect("home/show/$id","refresh");
					//$this->load->view('success');
				}
			}
		}
	}
                 
                public function show_pic()
                {
                          $id = $this->uri->segment(3);
                          $id=intval($id);
                          
                          $query = $this->db->select('title')->where('id',$id)->get('pic_title')->result_array();

                          if(0 == count($query)){
                              
                              redirect('home/index');
                          }
                          
                          $page = $this->uri->segment(4);
                          if(!$page){
                                      $page_offset=0;
                                       //redirect('home/index');
                          }
                           $page_offset = intval($page);
                           
                          if($page_offset>0){
                              $page_offset = $page-1;
                          }
                          
                          $title = $query[0]['title'];
                          
                          $arr_id = array("$id");
                          $res = $this->db->select('pid,image,des,created')->where_in('id',$arr_id)->limit(1,$page_offset)->get('pic')->result_array();
 
                         $counts = $this->db->where('id',$id)->count_all_results('pic');

                          $data = array();
                          $data['title'] = $title;
                          
                          $data['img'] = $res[0]['image'];
                          $data['des'] = $res[0]['des'];
                          if($page>0&$page>1){
                              $page = $page-1;               
                              $data['prec'] = base_url('home/show_pic').'/'.$id.'/'.$page;
                          }elseif($page==1){
                                             $data['prec'] = "#";
                          }
                          if($page>=$counts){
                                             $data['next'] ="#";
                          }else{
                              $page = $page+1;             
                              $data['next'] =base_url('home/show_pic').'/'.$id.'/'.$page;
                          }
                          
                          $this->load->view('home/header');
                          $this->load->view('home/show_pic',$data);
                           $this->load->view('home/footer');
                }
        
	public function error()
	{
		$str = '这是一段中文文字用来测试截取时是否会出现乱码';
		$str1 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
		echo mb_substr($str,0,10,'utf-8').'<br>';
		echo substr($str1,0,10).'<br>';
		
		//$this->load->view('error');
	}
}